#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define debug  freopen("in.txt","r",stdin),freopen("out.txt","w",stdout);
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 1e6+10;
using namespace std;

int N,K;
int f[maxn];
int h[maxn];
int main(){
    // debug;
    ios;
    cin>>N>>K;
    for(int i = 1;i<=N;i++) cin>>h[i];
    memset(f,0x3f,4*N+100);
    f[1] = 0;
    for(int i = 1;i<=N;i++){
        for(int j = 1;j<=K;j++){
            if(i+j<=N) f[i+j] = min(f[i+j],f[i] + abs(h[i+j] - h[i]));
        }
    }
    cout<<f[N]<<'\n';

    return 0;
}
